package top.hmtools.mapper.plus.mysql;

import java.util.Set;

import org.apache.ibatis.mapping.MappedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

public class UpdateMapperPlusProvider extends MapperTemplate {
	
	private final Logger logger = LoggerFactory.getLogger(UpdateMapperPlusProvider.class);

	public UpdateMapperPlusProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
		super(mapperClass, mapperHelper);
	}

	/**
	 * 修改一条数据，如果属性为 null ，或者 为空字符串，则忽略这些属性
	 * <br><b>注意入参异常造成误修改数据的危害</b>
	 * @param ms
	 * @return
	 */
	public String updateOneByPkIgnoreNullEmpty (MappedStatement ms){
		Class<?> entityClass = getEntityClass(ms);
	    StringBuilder sql = new StringBuilder();
	    sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
	    sql.append(SqlHelper.updateSetColumns(entityClass, "obj", true, true));
	    
	    sql.append(" where ");
	    
	    //获取所有该表的主键
	      Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(entityClass);
	      for(EntityColumn column:pkColumns){
	      	StringBuffer sqlPart = new StringBuffer();
	      	sqlPart.append("<if test=\" key == '{{key}}' \">");
	      	sqlPart.append("<if test=\" "+ "obj."+column.getProperty()+" != null and "+ "obj."+column.getProperty()+" != '' " +" \">");
	      	sqlPart.append(column.getColumn() + " = #{obj."+column.getProperty()+"}");
	      	sqlPart.append("</if>");
	      	sqlPart.append("</if>");
	      	sql.append(sqlPart.toString().replaceAll("\\{\\{key\\}\\}", column.getProperty()));
	      }
	      
	      if(this.logger.isDebugEnabled()){
	    	  this.logger.debug(Thread.currentThread().getStackTrace()[1].getMethodName()+"  :  "+sql.toString());
	      }
	      return sql.toString();
	}
	
	/**
	 * 修改一条数据，不忽略 null，空字符串
	 * <br><b>注意入参异常造成误修改数据的危害</b>
	 * @param ms
	 * @return
	 */
	public String updateOneByPk (MappedStatement ms){
		Class<?> entityClass = getEntityClass(ms);
		StringBuilder sql = new StringBuilder();
		sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
		sql.append(SqlHelper.updateSetColumns(entityClass, "obj", false, false));
		
		sql.append(" where ");
		
		//获取所有该表的主键
		Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(entityClass);
		for(EntityColumn column:pkColumns){
			StringBuffer sqlPart = new StringBuffer();
			sqlPart.append("<if test=\" key == '{{key}}' \">");
			sqlPart.append("<if test=\" "+ "obj."+column.getProperty()+" != null and "+ "obj."+column.getProperty()+" != '' " +" \">");
			sqlPart.append(column.getColumn() + " = #{obj."+column.getProperty()+"}");
			sqlPart.append("</if>");
			sqlPart.append("</if>");
			sql.append(sqlPart.toString().replaceAll("\\{\\{key\\}\\}", column.getProperty()));
		}
		
		if(this.logger.isDebugEnabled()){
			this.logger.debug(Thread.currentThread().getStackTrace()[1].getMethodName()+"  :  "+sql.toString());
		}
		return sql.toString();
	}
}
